package com.woniuxy.producer.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostMapping("/sendSimple")
    public String send(String msg){
        //发送消息队列
        rabbitTemplate.convertAndSend("simpleQueue", msg);
        return "ok";
    }

    /**
     * 发布订阅者模式,交换机类型：fanout
     * 交换机-fanout-exchange
     * 队列1、2
     */
    @PostMapping("/sendFanout")
    public String sendFanout(String msg){
        //交换机名称，路由器，消息内容
        rabbitTemplate.convertAndSend("fanout-exchange","",msg);
        return "ok";
    }

    /**
     *topic模式
     */
    @PostMapping("/sendTopic")
    public String sendTopic(String routingKey,String msg){
        //交换机名称，路由器，消息内容
        rabbitTemplate.convertAndSend("topic-exchange",routingKey,msg);
        return "ok";
    }
    /**
     * 消息确认
     */
    @PostMapping("/sendConfirm")
    public String sendConfirm(String exchange,String routingKey,String msg){
        //交换机名称，路由器，消息内容
        rabbitTemplate.convertAndSend(exchange,routingKey,msg);
        return "ok";
    }

    /**
     * 死信队列
     */
    @PostMapping("/sendDlx")
    public String sendDlx(String msg){
        //交换机名称，路由器，消息内容
        rabbitTemplate.convertAndSend("normal-exchange","city.gz",msg);
        return "ok";
    }
    // 延迟队列
    @PostMapping("sendDelay")
    public String sendDelay(String msg,long delayTimes){
        rabbitTemplate.convertAndSend("delayed-exchange","msg.delay",msg,message->{
            message.getMessageProperties().setHeader("x-delay",delayTimes);
            return message;
        });
        return "ok";
    }
}
